package au.edu.uq.itee.engg2800.team19;
import gnu.io.SerialPort;
import gnu.io.CommPortIdentifier;
public class RS232Conn extends java.util.Observable{

	private BaudRate baudRate;
	private SerialPort serialPort;
	private String commPort;
	private DataBits dataBits;
	private StopBits stopBits;

	private FlowControl flowControl;
	private int timeout;
	private FrequenciesReceived freqs;

	public boolean isConnected=false;

	public RS232Conn(String portName,BaudRate baudrate,DataBits databits,
			FlowControl flowControl,StopBits stopBits,int timeOut, FrequenciesReceived freqs) {
		this.commPort=portName;
		this.dataBits=databits;
		this.baudRate=baudrate;
		this.flowControl=flowControl;
		this.stopBits=stopBits;
		this.freqs=freqs;
	}
//
//	public RS232Conn(String portName,int baudrate,int databits,
//			int flowControl,int stopBits,int timeOut)  {
//		this.commPort=portName;
//		setDataBits(databits);
//		setBaudRate(baudrate);
//		setFlowControl(flowControl);
//		setStopBits(stopBits);
//		}

	public RS232Conn(FrequenciesReceived freqs)
	{

		this("COM1",BaudRate.BR_9600,DataBits.DATABITS_8,
				FlowControl.FLOWCONTROL_NONE,
				StopBits.SB_1,2000,freqs);

	}

	public boolean connect() throws Exception {

        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(commPort);

        if (portIdentifier.isCurrentlyOwned()) {
            return false;
        } else {
            // points who owns the port and connection timeout
        	serialPort = (SerialPort) portIdentifier.open("RS232Example", timeout);

            // setup connection parameters
            serialPort.setSerialPortParams(
                baudRate.getCode(), dataBits.getCode(),
                stopBits.getCode(),flowControl.getCode());

         // setup serial port writer
            DataSender.setWriterStream(serialPort.getOutputStream());

            // setup serial port reader
            new DataReceiver(serialPort.getInputStream(),freqs).start();
        }

		isConnected=true;
		System.out.println("connected");
		setChanged();
		notifyObservers();
		return true;
    }


	public DataBits getDataBits() {
		return dataBits;
	}


	public String getCommPort() {
		return commPort;
	}

	public int getTimeout() {
		return timeout;
	}

	public FlowControl getFlowControl() {
		return flowControl;
	}

	public StopBits getStopBits() {
		return stopBits;
	}

	public SerialPort getSerialPort(){
		return this.serialPort;
	}

	public void setSerialPort(SerialPort serialPort) {
		this.serialPort = serialPort;
	}

	public void setCommPort(String commPort) {
		this.commPort = commPort;
	}

	public void setDataBits(DataBits dataBits) {
		this.dataBits = dataBits;
	}

	public void setStopBits(StopBits stopBits) {
		this.stopBits = stopBits;
	}

	public void setBaudRate(BaudRate baudRate) {
		this.baudRate = baudRate;
	}

	public void setFlowControl(FlowControl flowControl) {
		this.flowControl = flowControl;
	}

	public void setDataBits(int dataBits) {
		this.dataBits = DataBits.valueOf(Integer.toString(dataBits));
	}

	public void setStopBits(int stopBits) {
		this.stopBits = StopBits.valueOf(Integer.toString(stopBits));
	}

	public void setBaudRate(int baudRate) {
		this.baudRate = BaudRate.valueOf(Integer.toString(baudRate));
	}

	public void setFlowControl(int flowControl) {
		this.flowControl = FlowControl.valueOf(Integer.toString(flowControl));
	}
	public void setSerialPort(String serialPort) {
		this.flowControl = FlowControl.valueOf(serialPort);
	}
}
